package com.example.patient.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.common.entity.EscortProduct;
import com.example.common.entity.EscortReserve;
import com.example.common.entity.EscortType;
import com.example.common.support.ApiException;
import com.example.common.utils.DateUtil;
import com.example.patient.constans.EscortReserveStatus;
import com.example.patient.dto.req.GetEscortProductList;
import com.example.patient.dto.resp.EscortProductList;
import com.example.patient.dto.resp.EscortReserveList;
import com.example.patient.dto.resp.EscortReserveVo;
import com.example.patient.dto.resp.EscortTypeList;
import com.example.patient.mapper.EscortProductMapper;
import com.example.patient.mapper.EscortReserveMapper;
import com.example.patient.mapper.EscortTypeMapper;
import com.example.patient.service.EscortReserveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * @ClassName EscortReserveServiceIpml
 * @Author wengjt
 * @Date 2021/5/7 10:12
 * @Description
 **/
@Service
public class EscortReserveServiceImpl implements EscortReserveService {
    @Resource
    private EscortReserveMapper escortReserveMapper;
    @Resource
    private EscortProductMapper escortProductMapper;
    @Resource
    private EscortTypeMapper escortTypeMapper;

    /**
     * 多条件查询陪护套餐列表
     *
     * @Author wengjt
     * @Date 14:24 2021/5/7
     */
    @Override
    public EscortProductList getEscortProductList(GetEscortProductList dto) {
        EscortProductList result = new EscortProductList();
        PageInfo<EscortProduct> pageInfo = PageHelper.startPage(dto.getPageNum(), dto.getPageSize()).doSelectPageInfo(() -> {
            escortProductMapper.multipleSearch(dto.getEscortProductName(), dto.getCtId(), dto.getShow());
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 查询自己的陪护订单
     *
     * @Author wengjt
     * @Date 11:37 2021/5/14
     */
    @Override
    public EscortReserveList getEscortReserveList(Long uid, Integer rStatus, Integer pageNum, Integer pageSize) {
        EscortReserveList result = new EscortReserveList();
        PageInfo<EscortReserveVo> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            escortReserveMapper.getPageByUid(uid, rStatus);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 根据id查询单个陪护套餐详细
     *
     * @Author wengjt
     * @Date 14:24 2021/5/7
     */
    @Override
    public EscortProduct getEscortProduct(Long cpId) {
        return escortProductMapper.findById(cpId);
    }

    /**
     * 多重条件获取陪护分类
     *
     * @Author wengjt
     * @Date 17:00 2021/5/7
     */
    @Override
    public EscortTypeList getEscortTypeList(Long etId, Integer showAble, Integer pageNum, Integer pageSize) {
        EscortTypeList result = new EscortTypeList();
        PageInfo<EscortType> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            escortTypeMapper.multipleSearch(etId, showAble);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    @Override
    public Object appointmentEscortReserve(Long uid, Long pId, Long productId, Date beginTime, Date endTime, String caregiverGender, String linkMan, String LinkTel) {

        EscortProduct escortProduct = escortProductMapper.findById(productId);
        if (escortProduct.getDisable() == 1) {
            throw new ApiException(400, "不能预约已经删除的陪护项目");
        }
        if (escortProduct.getShowAble() == 0) {
            throw new ApiException(400, "不能预约未上架的陪护项目");
        }
        //计算结束时间
        LocalDateTime begin = DateUtil.date2LocalDateTime(beginTime);
        endTime = DateUtil.localDateTime2Date(begin.plus(escortProduct.getLimitTime(), ChronoUnit.MINUTES));

        EscortReserve escortReserve = new EscortReserve();
        escortReserve.setErOrderNo(IdUtil.simpleUUID());
        escortReserve.setCreateTime(new Date());
        escortReserve.setPId(pId);
        escortReserve.setUid(uid);
        escortReserve.setProductId(productId);
        escortReserve.setBeginTime(beginTime);
        escortReserve.setEndTime(endTime);
        escortReserve.setCaregiverGender(caregiverGender);
        escortReserve.setLinkMan(linkMan);
        escortReserve.setLinkTel(LinkTel);
        escortReserve.setRStatusTiny(EscortReserveStatus.UN_PROCESS);
        escortReserve.setDisable(0);
        escortReserveMapper.insertOne(escortReserve);
        return null;
    }
}
